<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        
        // 获取今日开始和结束时间戳
        $todayStart = strtotime(date('Y-m-d 00:00:00'));
        $todayEnd = strtotime(date('Y-m-d 23:59:59'));
        
        // 获取本周开始和结束时间戳
        $weekStart = strtotime('this week Monday');
        $weekEnd = strtotime('this week Sunday 23:59:59');
        
        // 获取本月开始和结束时间戳
        $monthStart = strtotime(date('Y-m-01 00:00:00'));
        $monthEnd = strtotime(date('Y-m-t 23:59:59'));

        $this->view->assign([
            // 文旅订单统计
            'travel_today_count' => Db::name('travel_order_stats')->where('date', date('Y-m-d'))->value('order_count') ?: 0,
            'travel_today_amount' => Db::name('travel_order_stats')->where('date', date('Y-m-d'))->value('order_amount') ?: 0,
            'travel_week_count' => Db::name('travel_order_stats')->whereBetween('date', [date('Y-m-d', $weekStart), date('Y-m-d', $weekEnd)])->sum('order_count') ?: 0,
            'travel_week_amount' => Db::name('travel_order_stats')->whereBetween('date', [date('Y-m-d', $weekStart), date('Y-m-d', $weekEnd)])->sum('order_amount') ?: 0,
            'travel_month_count' => Db::name('travel_order_stats')->whereBetween('date', [date('Y-m-d', $monthStart), date('Y-m-d', $monthEnd)])->sum('order_count') ?: 0,
            'travel_month_amount' => Db::name('travel_order_stats')->whereBetween('date', [date('Y-m-d', $monthStart), date('Y-m-d', $monthEnd)])->sum('order_amount') ?: 0,
            'travel_total_count' => Db::name('travel_order_stats')->sum('order_count') ?: 0,
            'travel_total_amount' => Db::name('travel_order_stats')->sum('order_amount') ?: 0,
            
            // 商城订单统计
            'shop_today_count' => Db::name('shop_order_stats')->where('date', date('Y-m-d'))->value('order_count') ?: 0,
            'shop_today_amount' => Db::name('shop_order_stats')->where('date', date('Y-m-d'))->value('order_amount') ?: 0,
            'shop_week_count' => Db::name('shop_order_stats')->whereBetween('date', [date('Y-m-d', $weekStart), date('Y-m-d', $weekEnd)])->sum('order_count') ?: 0,
            'shop_week_amount' => Db::name('shop_order_stats')->whereBetween('date', [date('Y-m-d', $weekStart), date('Y-m-d', $weekEnd)])->sum('order_amount') ?: 0,
            'shop_month_count' => Db::name('shop_order_stats')->whereBetween('date', [date('Y-m-d', $monthStart), date('Y-m-d', $monthEnd)])->sum('order_count') ?: 0,
            'shop_month_amount' => Db::name('shop_order_stats')->whereBetween('date', [date('Y-m-d', $monthStart), date('Y-m-d', $monthEnd)])->sum('order_amount') ?: 0,
            'shop_total_count' => Db::name('shop_order_stats')->sum('order_count') ?: 0,
            'shop_total_amount' => Db::name('shop_order_stats')->sum('order_amount') ?: 0,
            
            // 服务订单统计
            'service_today_count' => Db::name('service_order_stats')->where('date', date('Y-m-d'))->value('order_count') ?: 0,
            'service_today_amount' => Db::name('service_order_stats')->where('date', date('Y-m-d'))->value('order_amount') ?: 0,
            'service_week_count' => Db::name('service_order_stats')->whereBetween('date', [date('Y-m-d', $weekStart), date('Y-m-d', $weekEnd)])->sum('order_count') ?: 0,
            'service_week_amount' => Db::name('service_order_stats')->whereBetween('date', [date('Y-m-d', $weekStart), date('Y-m-d', $weekEnd)])->sum('order_amount') ?: 0,
            'service_month_count' => Db::name('service_order_stats')->whereBetween('date', [date('Y-m-d', $monthStart), date('Y-m-d', $monthEnd)])->sum('order_count') ?: 0,
            'service_month_amount' => Db::name('service_order_stats')->whereBetween('date', [date('Y-m-d', $monthStart), date('Y-m-d', $monthEnd)])->sum('order_amount') ?: 0,
            'service_total_count' => Db::name('service_order_stats')->sum('order_count') ?: 0,
            'service_total_amount' => Db::name('service_order_stats')->sum('order_amount') ?: 0,

            // 用户统计
            'total_users' => Db::name('user')->count(), // 注册用户数
            'vip_users' => Db::name('user')->where('vip_expire_time', '>', time())->count(), // 有效会员数
            'expired_vip_users' => Db::name('user')->where('vip_expire_time', '<=', time())->where('vip_expire_time', '>', 0)->count(), // 过期会员数
            'teacher_users' => Db::name('user')->where('is_teacher', 1)->count(), // 老师用户数
        ]);

        return $this->view->fetch();
    }

}
